/**
 * 描述: 渠道标示列表页
 *
 * @author jiafei
 * @since 2021/12/06 11:23
 */
layui.extend({
    zmnExportUtil: '{/}/static/js/zmn.excel.util'
}).define(['index', 'form', 'table', 'laypage', 'layedit', 'zmnExportUtil'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var table = layui.table;
    var laypage = layui.laypage;
    var layedit = layui.layedit;
    let zmnExportUtil = layui.zmnExportUtil;

    let init = table.init('zmn-table', {
        toolbar: "#zmn-table-tool",
        defaultToolbar: ['exports'],
        page: true,
        limit: json.pageSize,
        height: 'full-149',
        done: function () {
            $("div[lay-event='LAYTABLE_EXPORT']").attr('lay-event', 'CUSTOM_EXPORT');
        }
    });
    laypage.render(layui.$.extend({limits: [10, 20, 30, 40, 50, 100]}, zmnPage.laypageOptions(json)));


    let ids = new Array();

    // 选择（全选或单选）
    table.on("checkbox(zmn-table)", function (obj) {
        ids = table.checkStatus('zmn-table').data.map((item) => item.markId);
    });

    // 启用/停用/新增
    table.on("toolbar(zmn-table)", function (obj) {

        //导出excel
        if (obj.event === 'CUSTOM_EXPORT') {

            let index = layer.load(1, {
                shade: [0.5, '#DBDBDB']
            });
            zmnUtil.downloadLimitByConfig("/channel/mark/outputExcel.action?" + $("#zmn-query").serialize(), "渠道标示.xlsx", function () {
                layer.close(index);
            });

            return false;
        }

        if (obj.event === "addEditView") {
            layer.open({
                type: 2,
                title: '新增',
                shadeClose: true,
                shade: 0.8,
                area: ['650px', '300px'],
                content: '/channel/mark/addEditView.action' //iframe的url
            });
            return true;
        }
        //启用停用
        if (obj.event != "start" && obj.event != "stop") {
            return false;
        }
        if (ids.length === 0) {
            return layer.alert("请选择至少1条数据！");
        }
        zmnPage.layStartStop(obj, "/channel/mark/modify/updateStatus.action", ids.join(","));
    });

    form.on("switch(chk_status)", function (obj) {
        console.log(obj);
        let url = "/channel/mark/modify/updateStatus.action";
        let channelId = obj.elem.getAttribute("key");
        let status = obj.elem.checked ? 2 : 1;
        switch (status) {
            case 1:
                zmnPage.ajaxSimple(url, {ids: channelId, status: 1}, 1000);
                break;
            case 2:
                zmnPage.ajaxSimple(url, {ids: channelId, status: 2}, 1000);
                break;
        }
        setTimeout(function () {
            location.reload(true);
        }, 3000);
    });

    // 修改排序
    table.on("edit(zmn-table)", function (obj) {
        if (obj.data.sort != null && obj.data.sort != "" && obj.field != 'simpleSpell') {
            zmnPage.laySortPositiveInteger(obj.value, "/channel/mark/modify/updateSort.action", obj.data.markId, 0, 1000, function () {
                if (obj.value.length > 8) {
                    return "排序字段长度不能超过8位";
                }
            });
        }
    });

    form.on('submit(zmn-form-reset)', function (obj) {
        form.val("zmn-query", {
            "keyword": "" // "name": "value"
            ,"channelKeyword": ""
            ,"status": ""
        });
        return false;
    });

    //对外暴露的接口
    exports('plat_channel_mark_list', {});
});

